home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / nurbs / curve.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.6 KB  |  172 lines

  1. /*
  2.  * Copyright 1993, 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* curve.c
  19.  * This program draws a rotating NURBS curve.
  20.  *
  21.  *    Escape key    - exit the program
  22.  */
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glut.h>
  26. #include <math.h>
  27. #include <stdio.h>
  28.  
  29. /*  Function Prototypes  */
  30.  
  31. GLvoid  initgfx( GLvoid );
  32. GLvoid  drawScene( GLvoid );
  33. GLvoid  reshape( GLsizei, GLsizei );
  34. GLvoid  animate( GLvoid );
  35. GLvoid  visibility( GLint );
  36. GLvoid  keyboard( GLubyte, GLint, GLint );
  37.  
  38. void printHelp( char * );
  39.  
  40. /* Global Definitions */
  41.  
  42. #define KEY_ESC    27    /* ascii value for the escape key */
  43.  
  44. /* Global Variables */
  45.  
  46. static GLUnurbsObj *theNurb;
  47.  
  48. static GLfloat angle = 0.0;
  49.  
  50. void
  51. main(int argc, char *argv[])
  52. {
  53.     GLsizei     width, height;
  54.  
  55.     glutInit( &argc, argv );
  56.  
  57.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  58.     height = glutGet( GLUT_SCREEN_HEIGHT );
  59.     glutInitWindowPosition( width/4, height/4 ); 
  60.     glutInitWindowSize( width/2, height/2 );
  61.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  62.     glutCreateWindow( argv[0] );
  63.     
  64.     initgfx();
  65.  
  66.     glutKeyboardFunc( keyboard );
  67.     glutReshapeFunc( reshape );
  68.     glutIdleFunc( animate ); 
  69.     glutVisibilityFunc( visibility ); 
  70.     glutDisplayFunc( drawScene ); 
  71.  
  72.     printHelp( argv[0] );
  73.  
  74.     glutMainLoop();
  75. }
  76.  
  77. void
  78. printHelp( char *progname )
  79. {
  80.     fprintf(stdout, "\n%s - draw a rotating NURBS curve\n"
  81.         "Escape key        - exit the program\n\n",
  82.         progname);
  83. }
  84.  
  85. GLvoid
  86. initgfx( GLvoid )
  87. {
  88.     glClearColor( 0, 0, 0, 1 );
  89.     glEnable( GL_DEPTH_TEST );
  90.  
  91.     /* Create NURBS object */
  92.     theNurb = gluNewNurbsRenderer();
  93.  
  94.     /* Set longest line segment to something lower than
  95.      * the default to get smooth looking curves.
  96.      */
  97.     gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 10.0);
  98. }
  99.  
  100. GLvoid 
  101. keyboard( GLubyte key, GLint x, GLint y )
  102. {
  103.     switch (key) {
  104.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  105.         exit(0);
  106.     }
  107. }
  108.  
  109. GLvoid
  110. animate( GLvoid )
  111. {
  112.     /* update the current angle */
  113.     angle = fmodf( (angle + 5.0), 360.0 );
  114.  
  115.     /* Tell GLUT to redraw the scene */
  116.     glutPostRedisplay();
  117. }
  118.         
  119. GLvoid
  120. visibility( int state )
  121. {
  122.     if (state == GLUT_VISIBLE) {
  123.         glutIdleFunc( animate );
  124.     } else {
  125.         glutIdleFunc( NULL );
  126.     }
  127. }
  128.  
  129. GLvoid
  130. reshape( GLsizei width, GLsizei height )
  131. {
  132.     GLdouble    aspect;
  133.  
  134.     glViewport( 0, 0, width, height );
  135.  
  136.     aspect = (GLdouble) width / (GLdouble) height;
  137.  
  138.     glMatrixMode( GL_PROJECTION );
  139.     glLoadIdentity();
  140.     gluPerspective( 45.0, aspect, 3.0, 20.0 );
  141.     glMatrixMode( GL_MODELVIEW );
  142.     glLoadIdentity();
  143.     glTranslatef( 0.0, 0.0, -5.0 ); 
  144. }
  145.  
  146. GLvoid
  147. drawScene( GLvoid )
  148. {
  149.     GLfloat curvePts[4][3] = {
  150.         {0.1, 0.5, 0.0}, {0.1, 0.9, 0.0}, 
  151.         {0.9, 0.9, 0.0}, {0.9, 0.5, 0.0}
  152.     };
  153.  
  154.         GLfloat curveKnots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
  155.  
  156.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  157.     
  158.     glColor3f( 0.0, 1.0, 0.0 );
  159.     glPushMatrix(); 
  160.     
  161.         glRotatef( angle, 0.0, 1.0, 0.0 );
  162.         gluBeginCurve( theNurb );
  163.             gluNurbsCurve( theNurb, 8, curveKnots, 3, 
  164.                 &curvePts[0][0], 4, GL_MAP1_VERTEX_3 );
  165.         gluEndCurve( theNurb );
  166.  
  167.     glPopMatrix();
  168.  
  169.     glutSwapBuffers();
  170. }
  171.  
  172.